package com.itheima.a01mystream;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.function.Consumer;
import java.util.function.IntFunction;

public class StreamDemo9 {
    public static void main(String[] args) {
        /**
         * forEach, count, toArray
         */

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周芷若", "赵敏", "张强", "张三丰", "张翠山", "张良", "王二麻子", "谢广坤");

        System.out.println("forEach");
        // void forEach(Consumer action) 遍历
        // Consumer的泛型：表示流中数据的类型
        // accept方法的形参s：依次表视流里面的每一个数据
        // 方法体：对每一个数据的处理操作
        // list.stream().forEach(new Consumer<String>() {
        //     @Override
        //     public void accept(String s) {
        //         System.out.println(s);
        //     }
        // });
        list.stream().forEach(s -> System.out.println(s));

        System.out.println("count");
        // long count 统计
        System.out.println(list.stream().count());

        System.out.println("toArray");
        // toArray 收集流中的数据，放到数组中
        /* 1
        Object[] objects = list.stream().toArray();
        System.out.println(Arrays.toString(objects));
        */
        /* 2
        String[] arr1 = list.stream().toArray(new IntFunction<String[]>() {
            @Override
            public String[] apply(int value) {
                return new String[value];
            }
        });
        System.out.println(Arrays.toString(arr1));
        */
        String[] arr2 = list.stream().toArray(value -> new String[value]);
        System.out.println(Arrays.toString(arr2));
    }
}
